package com.example.sfm.controller.teacher;

import com.example.sfm.constant.MessageConstants;
import com.example.sfm.pojo.dto.GradeStatisticsDto;
import com.example.sfm.pojo.dto.GradesDto;
import com.example.sfm.pojo.dto.GradesQueryDto;
import com.example.sfm.pojo.vo.GradeStatisticsVo;
import com.example.sfm.result.Result;
import com.example.sfm.service.IGradesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@RestController("userTeacherGradesController")
@RequestMapping("/user/teacher/grades")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "学生成绩管理", description = "教师对学生成绩的操作接口")
public class GradesController {
    private final IGradesService gradesService;

    /**
     * 分页查询成绩
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询学生成绩", description = "根据查询条件分页查询学生成绩信息")
    public Result<?> listGrades(@ParameterObject GradesQueryDto dto) {
        log.info("查询成绩列表: {}", dto);
        return Result.success(gradesService.listGrades(dto), MessageConstants.SELECT_OK);
    }

    /**
     * Excel导入成绩
     */
    @PostMapping("/import")
    @Operation(summary = "从 Excel 文件导入学生成绩", description = "上传 Excel 文件并导入学生成绩信息")
    public Result<?> importExcel(@Parameter(description = "Excel 文件") @RequestParam("file") @NotNull MultipartFile file) {
        log.info("导入成绩文件: {}", file.getOriginalFilename());
        gradesService.importFromExcel(file);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * Excel导出成绩
     */
    @GetMapping("/export")
    @Operation(summary = "导出学生成绩到 Excel", description = "根据查询条件导出学生成绩信息到 Excel 文件")
    public void exportExcel(@Parameter(description = "HTTP 响应对象") HttpServletResponse response,
                            @ParameterObject GradesQueryDto dto) {
        log.info("导出成绩数据: {}", dto);
        gradesService.exportExcel(response, dto);
    }

    /**
     * 创建成绩记录
     */
    @PostMapping("/create")
    @Operation(summary = "创建学生成绩记录", description = "创建一条新的学生成绩记录")
    public Result<?> createGrade(@Parameter(description = "成绩信息") @RequestBody @Validated GradesDto dto) {
        log.info("创建成绩: {}", dto);
        gradesService.createGrade(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    /**
     * 更新成绩记录
     */
    @PutMapping("/update/{id}")
    @Operation(summary = "更新学生成绩记录", description = "根据成绩记录 ID 更新学生成绩信息")
    public Result<?> updateGrade(@Parameter(description = "成绩记录 ID",in = ParameterIn.PATH) @PathVariable Integer id,
                                 @Parameter(description = "成绩信息更新参数") @RequestBody @Validated GradesDto dto) {
        log.info("更新成绩: ID={}, 内容={}", id, dto);
        gradesService.updateGrade(id, dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 教师统计学生成绩数据接口
     *
     * @return 统计结果
     */
    @GetMapping("/statistics")
    @Operation(summary = "统计学生成绩数据", description = "根据统计条件统计学生成绩数据")
    public Result<?> getGradeStatistics(@Parameter(description = "成绩统计参数") GradeStatisticsDto dto) {
        log.info("教师统计成绩数据: {}", dto);
        GradeStatisticsVo statistics = gradesService.getGradeStatistics(dto);
        return Result.success(statistics, MessageConstants.SELECT_OK);
    }
}